package com.chenjt.juc.two_completableFuture;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/7/6 9:33
 */
public class FutureAPIDemo {


    public static void main(String[] args) throws Exception {
        FutureTask futureTask = new FutureTask<String>(()->{
            TimeUnit.SECONDS.sleep(5);
            return "task over";
        });

        Thread t1 = new Thread(futureTask, "t1");
        t1.start();

        System.out.println(Thread.currentThread().getName() + "\t ---------忙其他任务了");

//        System.out.println(futureTask.get());
//        System.out.println(futureTask.get(3,TimeUnit.SECONDS));

        // 如果要用futureTask，这段要加上，直接get容易造成阻塞
        while (true){
            if(futureTask.isDone()){
                System.out.println(futureTask.get());
                break;
            }else{
                TimeUnit.MILLISECONDS.sleep(500);
                System.out.println("正在处理中，不要再催了，越催越慢，再催熄火");
            }
        }




    }

}
